let Home = {
    baseUrl: 'http://81.71.65.4:3003',
    req: /^1[3,5,7,9]\d{9}$/,
    sendPhone() {
        document.querySelector('.sj').value = '17670918781'
        document.querySelector('.fs').addEventListener('click', () => {

            let phcode = document.querySelector('.sj').value.trim()
            if (!Home.req.test(phcode)) {
                document.querySelector('.sj').value = ''
                return alert('请输入正确的手机号码')
            }

            axios.get(this.baseUrl + `/user/getSmsCode?phone=${phcode}`).then((res) => {
                // console.log(res);
                let data = res.data
                alert(data.msg)
                document.querySelector('.yzm').value = data.smsCode
            }).catch(() => {
                alert('请求接口错误')
            })
        })
    },

    login() {
        document.querySelector('[type=button]').addEventListener('click', () => {
            let phcode = document.querySelector('.sj').value.trim()
            let sms = document.querySelector('.yzm').value.trim()
            axios.post(this.baseUrl + '/user/login', { phone: phcode, smsCode: sms }).then((res) => {
                    // console.log(res);
                    alert(res.data.msg)
                    localStorage.setItem('token', res.data.result.token)
                    localStorage.setItem('uname', res.data.result.phone)
                    location.href = 'index.html'
                }).catch(() => {
                    alert('验证码错误')
                })
                // console.log(phcode, sms);
        })
    }

}

onload = function() {
    Home.sendPhone()
    Home.login()
}